package com.kws.common.core.sys;

import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * <b>Ksys</b>
 *
 * @description: Ksys <br>
 * @date: 2024/10/23 17:29 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
public class Ksys {
    private static final Logger logger = LoggerFactory.getLogger(Ksys.class);

    public static void setBasicModelData(Object model, String user, String enable, Date sysTime, ModelOpType op) {
        if (StringUtils.isBlank(user)) {
            user = "[SYS]";
        }

        Class clazz = model.getClass();

        try {
            Method getEnable = clazz.getDeclaredMethod("getEnable");
            Method setEnable = clazz.getDeclaredMethod("setEnable", String.class);
            Method setCreateTime = clazz.getDeclaredMethod("setCreateTime", Date.class);
            Method setUpdateTime = clazz.getDeclaredMethod("setUpdateTime", Date.class);
            Method setCreateUser = clazz.getDeclaredMethod("setCreateUser", String.class);
            Method setUpdateUser = clazz.getDeclaredMethod("setUpdateUser", String.class);
            Object enableValue = getEnable.invoke(model);
            if (enableValue == null) {
                setEnable.invoke(model, enable);
            }

            if (op == ModelOpType.CREATE) {
                setCreateTime.invoke(model, sysTime);
                setCreateUser.invoke(model, user);
            }

            setUpdateTime.invoke(model, sysTime);
            setUpdateUser.invoke(model, user);
        } catch (Exception var13) {
            logger.warn("Model基础信息设置失败，请检查五个共通字段 {}", model.getClass().getName());
        }
    }

    public static void setBasicModelData(Object model, String user, String enable, ModelOpType op) {
        Date now = new Date();
        setBasicModelData(model, user, enable, now, op);
    }
}
